using System.IO;
using UnityEditor;
using UnityEngine;
using PackageInfo = UnityEditor.PackageManager.PackageInfo;

namespace Jerry.UiFrame.Editor
{
    public class CreateUI
    {
        private static string _pixPath = "Editor/UIPrefabs/";
        static GameObject _uiCanvas;

        /// <summary>
        /// 创建2DCanvas
        /// </summary>
        static void Create2DCanvas()
        {
            _pixPath = GetPackageFullPath(out int type);
            
            _uiCanvas = GameObject.Find("2DCanvas");
            if (_uiCanvas == null)
            {
                GameObject prefab = null;

                if (type == 2)
                {
                    prefab = AssetDatabase.LoadAssetAtPath<GameObject>(_pixPath + "2DCanvas.prefab");
                }
                else if (type == 1)
                {
                    prefab = LoadFromPackage<GameObject>(_pixPath + "2DCanvas.prefab");
                }
                
                   
                if (prefab == null)
                {
                    
                }
                _uiCanvas = Object.Instantiate(prefab) as GameObject;
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="parent"></param>
        /// <param name="path"></param>
        /// <returns></returns>
        static void CreateComponent(Transform parent, string path)
        {
            _pixPath = GetPackageFullPath(out int type);
            var prefab = AssetDatabase.LoadAssetAtPath<GameObject>(_pixPath + path + ".prefab");
            var go = Object.Instantiate(prefab, parent) as GameObject;
            go.GetComponent<RectTransform>().anchoredPosition = Vector3.zero;
            go.GetComponent<RectTransform>().localScale = Vector3.one;
            Selection.activeTransform = go.transform;
        }

        /// <summary>
        /// 获取资源路径
        /// </summary>
        /// <returns></returns>
        static string GetPackageFullPath(out int type)
        {
            string packagePath = Path.GetFullPath("Packages/com.jerry.uiframe");
            if (Directory.Exists(packagePath))
            {
                type = 1;
                return "Packages/com.jerry.uiframe/Editor/UIPrefabs/";
            }

            packagePath = Path.GetFullPath("Assets/..");
            if (Directory.Exists(packagePath))
            {
                // Search default location for development package
                if (Directory.Exists(packagePath + "/Assets/UIFrame"))
                {
                    type = 2;
                    return "Assets/UIFrame/Editor/UIPrefabs/";
                }
            }

            type = -1;
            return null;
        }

        /// <summary>
        /// 从Package中加载资源
        /// </summary>
        static T LoadFromPackage<T>(string path) where T : Object
        {
            var packageInfo = PackageInfo.FindForAssetPath($"Packages/com.jerry.uiframe");
            if (packageInfo == null) return null;
            var guid = AssetDatabase.AssetPathToGUID(path);
            Debug.Log(guid);
            return AssetDatabase.LoadAssetAtPath<T>(path);
        }
        
        [MenuItem("GameObject/创建UI/UI容器", false, -1111)]
        static void CreatePanel()
        {
            var parent = Selection.activeTransform;
            if (_uiCanvas == null)
            {
                Create2DCanvas();
                parent = _uiCanvas.transform.Find("Canvas");
            }

            if (parent.gameObject.name == "Canvas")
            {
                
            }
            
            CreateComponent(parent, "UITestWin");
        }
        
        [MenuItem("GameObject/创建UI/组件容器", false, -1)]
        static void CreateUiPanel()
        {
            var parent = Selection.activeTransform;
            if (parent == null)
            {
                Debug.LogError("请选中一个物体！！！");
                return;
            }
            
            CreateComponent(parent, "Panel_Parent");
        }

        [MenuItem("GameObject/创建UI/按钮/普通", false, 0)]
        static void CreateButton()
        {
            var parent = Selection.activeTransform;
            if (parent == null)
            {
                Debug.LogError("请选中一个物体！！！");
                return;
            }

            CreateComponent(parent, "Btn_Test");
        }

        [MenuItem("GameObject/创建UI/按钮/TMP", false, 1)]
        static void CreateButtonTMP()
        {
            var parent = Selection.activeTransform;
            if (parent == null)
            {
                Debug.LogError("请选中一个物体！！！");
                return;
            }

            CreateComponent(parent, "Btn_TMPTest");
        }

        [MenuItem("GameObject/创建UI/图片", false, 2)]
        static void CreateImage()
        {
            var parent = Selection.activeTransform;
            if (parent == null)
            {
                Debug.LogError("请选中一个物体！！！");
                return;
            }

            CreateComponent(parent, "Img_Test");
        }

        [MenuItem("GameObject/创建UI/列表", false, 3)]
        static void CreateList()
        {
            var parent = Selection.activeTransform;
            if (parent == null)
            {
                Debug.LogError("请选中一个物体！！！");
                return;
            }

            CreateComponent(parent, "List_Test");
        }

        [MenuItem("GameObject/创建UI/滑动条", false, 4)]
        static void CreateSlider()
        {
            var parent = Selection.activeTransform;
            if (parent == null)
            {
                Debug.LogError("请选中一个物体！！！");
                return;
            }

            CreateComponent(parent, "Slider_Test");
        }

        [MenuItem("GameObject/创建UI/文本/普通文本", false, 5)]
        static void CreateText()
        {
            var parent = Selection.activeTransform;
            if (parent == null)
            {
                Debug.LogError("请选中一个物体！！！");
                return;
            }

            CreateComponent(parent, "Text_Test");
        }

        [MenuItem("GameObject/创建UI/文本/TextMeshPro", false, 6)]
        static void CreateTMP()
        {
            var parent = Selection.activeTransform;
            if (parent == null)
            {
                Debug.LogError("请选中一个物体！！！");
                return;
            }

            CreateComponent(parent, "TMP_Test");
        }

        [MenuItem("GameObject/创建UI/多选", false, 7)]
        static void CreateToggles()
        {
            var parent = Selection.activeTransform;
            if (parent == null)
            {
                Debug.LogError("请选中一个物体！！！");
                return;
            }

            CreateComponent(parent, "Toggle_Multi");
        }

        [MenuItem("GameObject/创建UI/单选", false, 8)]
        static void CreateSingleToggle()
        {
            var parent = Selection.activeTransform;
            if (parent == null)
            {
                Debug.LogError("请选中一个物体！！！");
                return;
            }

            CreateComponent(parent, "Toggle_Single");
        }
        
        [MenuItem("GameObject/创建UI/输入框/普通文本", false, 9)]
        static void CreateInputField()
        {
            var parent = Selection.activeTransform;
            if (parent == null)
            {
                Debug.LogError("请选中一个物体！！！");
                return;
            }

            CreateComponent(parent, "Input_Test");
        }
        
        [MenuItem("GameObject/创建UI/输入框/TextMeshPro", false, 10)]
        static void CreateTMPInputField()
        {
            var parent = Selection.activeTransform;
            if (parent == null)
            {
                Debug.LogError("请选中一个物体！！！");
                return;
            }

            CreateComponent(parent, "InputTMP_Test");
        }
        
        [MenuItem("GameObject/创建UI/下拉框/普通文本", false, 11)]
        static void CreateDropdown()
        {
            var parent = Selection.activeTransform;
            if (parent == null)
            {
                Debug.LogError("请选中一个物体！！！");
                return;
            }

            CreateComponent(parent, "Drop_Test");
        }
        
        [MenuItem("GameObject/创建UI/下拉框/TextMeshPro", false, 12)]
        static void CreateTMPDropdown()
        {
            var parent = Selection.activeTransform;
            if (parent == null)
            {
                Debug.LogError("请选中一个物体！！！");
                return;
            }

            CreateComponent(parent, "DropTMP_Test");
        }
    }
}